﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Bixas.Tangibilis.Controls
{
    public class RandomPanel : Panel
    {
        private bool isFirstArrange = true;
        
        protected override Size MeasureOverride(Size availableSize)
        {
            Size infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

            foreach (UIElement child in Children)
            {
                child.Measure(infiniteSize);
            }

            return availableSize;
        }

        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
        {
            
            if (this.Children == null || this.Children.Count == 0 || !isFirstArrange)
                return finalSize;

            Random rnd = new Random();
           
            double curX = 0, curY = 0;

            foreach (UIElement child in Children)
            {
                curY = rnd.Next((int)finalSize.Height);
                curX = rnd.Next((int)finalSize.Width);


                child.Arrange(new Rect(curX, curY, child.DesiredSize.Width,
                    child.DesiredSize.Height));
            }

            isFirstArrange = false;
            return finalSize;

        }
    }
}
